/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.femass.relatorios;

import com.femass.controller.GestaoEventos;
import com.femass.model.Artigo;
import com.femass.model.Evento;
import com.femass.util.ConnectionFactory;
import com.femass.util.FacesUtil;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
 *
 * @author daureo
 */
public class GerarRelatorios {

    public String artigosPorEvento(Evento evento) throws JRException, SQLException {

        GestaoEventos gestaoEventos = new GestaoEventos();
        List<Artigo> artigos = gestaoEventos.porCodigo(evento.getCod()).getArtigoList();
        if (artigos.isEmpty()) {
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_FATAL, "Não existem Artigos cadastrados neste evento!");
        } else {

            Map parametros = new HashMap();
            FacesContext facesContext = FacesContext.getCurrentInstance();
            facesContext.responseComplete();
            ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();

            parametros.put("EVENTO_COD", evento.getCod());


            JasperReport pathjrxml = JasperCompileManager.compileReport(
                    scontext.getRealPath("/relatorios/relatorioArtigosPorEvento.jrxml"));

            JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                    ConnectionFactory.getSakilaConnection());

            JasperExportManager.exportReportToPdfFile(printReport,
                    (String) scontext.getRealPath("/relatorios/") + "/relatorioArtigosPorEvento.pdf");

            ConnectionFactory.getSakilaConnection().close();
            return "../relatorios/relatorioArtigosPorEvento.pdf";
        }
        return null;
    }
    
    public String confirmadosPorEvento(Evento evento) throws JRException, SQLException {

        GestaoEventos gestaoEventos = new GestaoEventos();
        //List<Inscricao> participantes = gestaoEventos.porCodigo(evento.getCod()).getInscricaoList();
        
        if (1==0) {
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_FATAL, "Não existem Participantes confirmados neste evento!");
        } else {

            Map parametros = new HashMap();
            FacesContext facesContext = FacesContext.getCurrentInstance();
            facesContext.responseComplete();
            ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();

            parametros.put("EVENTO_COD", evento.getCod());


            JasperReport pathjrxml = JasperCompileManager.compileReport(
                    scontext.getRealPath("/relatorios/relatorioTotalConfirmacao.jrxml"));

            JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                    ConnectionFactory.getSakilaConnection());

            JasperExportManager.exportReportToPdfFile(printReport,
                    (String) scontext.getRealPath("/relatorios/") + "/relatorioTotalConfirmacao.pdf");

            ConnectionFactory.getSakilaConnection().close();
            return "../relatorios/relatorioTotalConfirmacao.pdf";
        }
        return null;
    }
    
    public String alunosPorEvento(Evento evento) throws JRException, SQLException {

        GestaoEventos gestaoEventos = new GestaoEventos();
        //List<Inscricao> participantes = gestaoEventos.porCodigo(evento.getCod()).getInscricaoList();
        
        if (1==0) {
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_FATAL, "Não existem Alunos confirmados neste evento!");
        } else {

            Map parametros = new HashMap();
            FacesContext facesContext = FacesContext.getCurrentInstance();
            facesContext.responseComplete();
            ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();

            parametros.put("EVENTO_COD", evento.getCod());


            JasperReport pathjrxml = JasperCompileManager.compileReport(
                    scontext.getRealPath("/relatorios/relatorioAlunosConfirmados.jrxml"));

            JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                    ConnectionFactory.getSakilaConnection());

            JasperExportManager.exportReportToPdfFile(printReport,
                    (String) scontext.getRealPath("/relatorios/") + "/relatorioAlunosConfirmados.pdf");

            ConnectionFactory.getSakilaConnection().close();
            return "../relatorios/relatorioAlunosConfirmados.pdf";
        }
        return null;
    }
}
